//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');
const post = util.post;
var getInfo = util.getInfo;
var coupon_image = util.coupon_image;
const jumpWeapp = util.appid;
const getCurrentPageUrlWithArgs = util.getCurrentPageUrlWithArgs;
Page({
    data: {
        background: [{
                image: "../../image/slide2.png"
            },
            {
                image: "../../image/slide3.png"
            }
        ],
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 2000,
        duration: 500,

        windowWidth: 0,

        // tabs: ['综合', '佣金', '销量', '价格'],
        tabs: [{
                name: '综合',
                order: '',
                status: 0
            },
            {
                name: '佣金',
                order: 'yongjin',
                status: 0
            },
            {
                name: '销量',
                order: 'sum',
                status: 0
            },
            {
                name: '价格',
                order: 'price',
                status: 0
            }
        ],
        nowTab: '',
        sort: '',
        stv: {
            windowWidth: 0,
            lineWidth: 0,
            offset: 0,
            tStart: false
        },
        activeTab: 0,

        category: [
            [{
                    id: "18",
                    title: "电器",
                    image: "../../image/electric.png"
                },
                {
                    id: "4",
                    title: "母婴",
                    image: "../../image/baby.png"
                },
                {
                    id: "16",
                    title: "美妆",
                    image: "../../image/beauty.png"
                },
                {
                    id: "818",
                    title: "家纺",
                    image: "../../image/house.png"
                },
                {
                    id: "1451",
                    title: "运动",
                    image: "../../image/bras.png"
                },
                {
                    id: "743",
                    title: "男装",
                    image: "../../image/man.png"
                }
            ],
            [

                {
                    id: "1",
                    title: "美食",
                    image: "../../image/food.png"
                },
                {
                    id: "1543",
                    title: "手机",
                    image: "../../image/digit.png"
                },
                {
                    id: "1281",
                    title: "鞋包",
                    image: "../../image/shoes.png"
                },
                {
                    id: "15",
                    title: "百货",
                    image: "../../image/other.png"
                },
                {
                    id: "13",
                    title: "水果",
                    image: "../../image/fruit.png"
                },
                {
                    id: "12",
                    title: "海淘",
                    image: "../../image/haitao.png"
                }
            ]
        ],

        goods: [], // 商品列表
        page: 1, // 当前页码
        fid: '', // 商品种类
        isEnd: false,
        keyword: '', // 搜索词
        sort: 'desc',

        goods_item_padding: 3,
        coupon_image: coupon_image,

        show_toast: false,
        show_toast2: false,

        _fid: 0,
        ad_flag: 0, //是否显示广告页
        ad_info: null, //广告数据
        zhuanke: 0,
        hahah: true
    },
    onShow() {
        // wx.showTabBar();
        let that = this;
        app.ifuserinfo();
        // if (that.data.zhuanke == 1 && 1 == that.data.weapp_status) {
        //     wx.navigateTo({
        //         url: that.data.zhuanke_path,
        //     });
        // } else {
        //     that.checkLogin();
        // };
        let p0 = {
            m: 'weapp_status',
            v: 4
        };
        post(p0).then(res => {
            that.setData({
                weapp_status: res.status
            });
            if (1 == that.data.weapp_status) { // 审核通过了 
                var weapp_openid = wx.getStorageSync('weapp_openid');
                if (!!weapp_openid) {
                    let pp = {
                        m: 'usercheck',
                        openid: weapp_openid,
                        jump_weapp: jumpWeapp
                    };
                    post(pp).then(res => {
                        // console.log(res);
                        if (0 == res.r) {
                            wx.getStorageSync('z_uid', res.z_uid);
                            wx.getStorageSync('m_uid', res.m_uid);
                            wx.navigateTo({
                                url: '../../pages/cleanUp/cleanUp?z_uid=' + res.z_uid + '&m_uid=' + res.m_uid,
                            });
                        } else {
                            wx.navigateTo({
                                url: '../../pages/more/more'
                            });
                        }
                    })
                } else {
                    var z_uid = wx.getStorageSync('z_uid');
                    var m_uid = wx.getStorageSync('m_uid');
                    if (!!z_uid && !!m_uid) {
                        if (z_uid !== '' && m_uid !== '') {
                            wx.navigateTo({
                                url: '../../pages/cleanUp/cleanUp?z_uid=' + z_uid + '&m_uid=' + m_uid,
                            });
                        }
                    } else {
                        that.getList();
                    }
                }
            } else {
                that.getList();
            }
        });

    },

    getList() {
        let that = this;
        try {
            let {
                tabs
            } = that.data;
            var res = wx.getSystemInfoSync()
            that.windowWidth = res.windowWidth;
            that.data.stv.lineWidth = that.windowWidth / that.data.tabs.length;
            that.data.stv.windowWidth = res.windowWidth;
            that.setData({
                stv: that.data.stv
            })
            that.tabsCount = tabs.length;
        } catch (e) {}
        that.setData({
            windowWidth: wx.getSystemInfoSync().windowWidth, //图片宽度
        });
        // 请求页面加载默认展示的数据
        var defaultUrl = app.globalData.commonUrl;
        var p = {
            m: 'index',
            uid: 2
        }
        getInfo(p).then(res => {
            that.setData({
                goods: res.result
            }, () => {

            });
            if (wx.getStorageSync('count') <= 5) {
                // wx.showToast({
                //     title: '点击客服按钮，可领取两元现金红包',
                //     icon: 'none',
                //     duration: 3000,
                //     mask: true
                // });
                var info = wx.getSystemInfoSync().system.toLowerCase();
                if (/^ios/.test(info)) {} else if (/^android/.test(info)) {
                    that.setData({
                        show_toast: true
                    })

                }
            }
        })
    },
    onLoad: function(options) {
        var that = this;

    },
    _updateSelectedPage(page) {
        let {
            tabs,
            stv,
            activeTab
        } = this.data;
        activeTab = page;
        this.setData({
            activeTab: activeTab
        })
        stv.offset = stv.windowWidth * activeTab;
        this.setData({
            stv: this.data.stv
        })
    },
    //tab选项卡
    handlerTabTap(e) {
        this._updateSelectedPage(e.currentTarget.dataset.index);
        //商品排序
        var index = e.currentTarget.dataset.index;
        var item = e.currentTarget.dataset.item;
        //console.log(this.data.nowTab);
        var that = this;

        let p = {};
        that.data.page = 1;

        if (item.name == '价格') {
            if (item.name == that.data.nowTab.name) {
                if (item.status == 0) {
                    item.status = 1;
                    that.setData({
                        tabs: [{
                                name: '综合',
                                order: '',
                                status: 0
                            },
                            {
                                name: '佣金',
                                order: 'yongjin',
                                status: 0
                            },
                            {
                                name: '销量',
                                order: 'sum',
                                status: 0
                            },
                            {
                                name: '价格',
                                order: 'price',
                                status: 1
                            }
                        ]
                    })
                    that.data.sort = 'desc';
                    p = {
                        m: 'index',
                        fid: that.data.fid ? that.data.fid : '',
                        search: that.data.keyword ? that.data.keyword : '',
                        order: item.order,
                        uid: 2,
                        sort: 'desc'
                    }
                } else if (item.status == 1) {
                    item.status = 2;
                    that.setData({
                        tabs: [{
                                name: '综合',
                                order: '',
                                status: 0
                            },
                            {
                                name: '佣金',
                                order: 'yongjin',
                                status: 0
                            },
                            {
                                name: '销量',
                                order: 'sum',
                                status: 0
                            },
                            {
                                name: '价格',
                                order: 'price',
                                status: 2
                            }
                        ]
                    })
                    that.data.sort = 'asc';
                    p = {
                        m: 'index',
                        fid: that.data.fid ? that.data.fid : '',
                        search: that.data.keyword ? that.data.keyword : '',
                        order: item.order,
                        uid: 2,
                        sort: 'asc'
                    }
                } else if (item.status == 2) {
                    item.status = 1;
                    that.data.sort = 'desc';
                    that.setData({
                        tabs: [{
                                name: '综合',
                                order: '',
                                status: 0
                            },
                            {
                                name: '佣金',
                                order: 'yongjin',
                                status: 0
                            },
                            {
                                name: '销量',
                                order: 'sum',
                                status: 0
                            },
                            {
                                name: '价格',
                                order: 'price',
                                status: 1
                            }
                        ]
                    })
                    p = {
                        m: 'index',
                        fid: that.data.fid ? that.data.fid : '',
                        search: that.data.keyword ? that.data.keyword : '',
                        order: item.order,
                        uid: 2,
                        sort: 'desc'
                    }
                }
            } else {
                that.setData({
                    tabs: [{
                            name: '综合',
                            order: '',
                            status: 0
                        },
                        {
                            name: '佣金',
                            order: 'yongjin',
                            status: 0
                        },
                        {
                            name: '销量',
                            order: 'sum',
                            status: 0
                        },
                        {
                            name: '价格',
                            order: 'price',
                            status: 0
                        }
                    ]
                });
                p = {
                    m: 'index',
                    fid: that.data.fid ? that.data.fid : '',
                    search: that.data.keyword ? that.data.keyword : '',
                    order: item.order,
                    uid: 2
                }
            }
        } else {
            that.setData({
                tabs: [{
                        name: '综合',
                        order: '',
                        status: 0
                    },
                    {
                        name: '佣金',
                        order: 'yongjin',
                        status: 0
                    },
                    {
                        name: '销量',
                        order: 'sum',
                        status: 0
                    },
                    {
                        name: '价格',
                        order: 'price',
                        status: 0
                    }
                ]
            });
            that.data.sort = 'desc';
            p = {
                m: 'index',
                order: item.order,
                fid: that.data.fid ? that.data.fid : '',
                sort: 'desc',
                uid: 2,
                search: that.data.keyword ? that.data.keyword : ''
            }
        }
        getInfo(p).then(res => {
            that.setData({
                goods: res.result
            })
        })


        that.setData({
            tabs: that.data.tabs
        });
        that.data.nowTab = item;
        that.setData({
            nowTab: item
        })
    },

    getUserInfo: function(e) {
        //console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },

    /**
     * item点击事件
     */
    onIpItemClick: function(event) {
        //console.log(event);
        var id = event.currentTarget.dataset.item.id;
        var curIndex = 0;
        for (var i = 0; i < this.data.ips.length; i++) {
            if (id == this.data.ips[i].id) {
                this.data.ips[i].isSelect = true;
                curIndex = i;
            } else {
                this.data.ips[i].isSelect = false;
            }
        }

        this.setData({
            ips: this.data.ips,
        });
    },

    // 触底加载更多
    onReachBottom() {
        //console.log(this.data.nowPage);
        var defaultUrl = app.globalData.commonUrl;
        var that = this;
        that.data.page++;
        let p = {
            m: 'index',
            fid: that.data.fid ? that.data.fid : '',
            search: that.data.keyword ? that.data.keyword : '',
            order: that.data.nowTab.order ? that.data.nowTab.order : '',
            uid: 2,
            sort: that.data.sort,
            page: that.data.page
        };
        getInfo(p).then(res => {
            that.setData({
                goods: [...that.data.goods, ...res.result]
            })
        })
    },
    // 选择分类
    chooseCategory(e) {
        this.setData({
            tabs: [{
                    name: '综合',
                    order: '',
                    status: 0
                },
                {
                    name: '佣金',
                    order: 'yongjin',
                    status: 0
                },
                {
                    name: '销量',
                    order: 'sum',
                    status: 0
                },
                {
                    name: '价格',
                    order: 'price',
                    status: 0
                }
            ]
        });
        this._updateSelectedPage(0);
        var fid = e.currentTarget.dataset.id;
        this.data.fid = fid;
        this.data.page = 1;
        this.data.keyword = '';
        var that = this;
        //console.log(fid);
        const p = {
            m: 'index',
            fid: fid,
            uid: 2
        };
        getInfo(p).then(res => {
            //console.log(res);
            that.setData({
                goods: res.result,
                _fid: fid
            })
        })
    },
    copy_words(e) {
        // 保存输入的关键词
        this.data.keyword = e.detail.value;
    },
    // 点击回车搜索商品
    searchGoods(e) {
        var keyword = e.detail.value;
        this.data.keyword = keyword;
        var that = this;
        this.search();
    },
    // 点击图标按钮搜索商品
    search_btn() {
        this.search();
    },
    // 搜索
    search() {
        var that = this;
        this.data.fid = '';
        this.data.page = 1;
        let p = {
            m: 'index',
            search: that.data.keyword,
            uid: 2
        };
        getInfo(p).then(res => {
            that.setData({
                goods: res.result
            })
        })
    },
    // onShareAppMessage: function(res) {
    //   if (res.from === 'button') {
    //     // 来自页面内转发按钮
    //     console.log(res.target)
    //   }
    //   return {
    //     title: '桔子派',
    //     path: 'pages/index/index',
    //     success: function(res) {
    //       // 转发成功
    //     },
    //     fail: function(res) {
    //       // 转发失败
    //     }
    //   }
    // },
    close_toast() {
        let that = this;
        that.setData({
            show_toast: false
        })
    },
    close_toast2() {
        let that = this;
        that.setData({
            show_toast2: false
        })
    },
    openShare() {
        // wx.hideTabBar();
        let that = this;
        that.setData({
            show_toast2: true
        })
    },
    onReady: function() {
        let that = this;
        if (that.data.goods.length == 0) {
            that.setData({
                hahah: false
            })
        }
    }
})